home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BPUTHF.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  133 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bputhf.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* local headers */
  18. #include "blkio_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      bputhf - put a header field into a block file
  23.  
  24. SYNOPSIS
  25.      #include <blkio.h>
  26.  
  27.      int bputhf(bp, offset, buf, bufsize)
  28.      BLKFILE *bp;
  29.      size_t offset;
  30.      const void *buf;
  31.      size_t bufsize;
  32.  
  33. DESCRIPTION
  34.      The bputhf function writes the contents of buf into a field of
  35.      the header of the block file associated with BLKFILE pointer bp.
  36.      The field begins offset characters from the beginning of the
  37.      header and is bufsize characters long.  buf must point to a
  38.      storage area at least bufsize characters long.
  39.  
  40.      bputhf will fail if one or more of the following is true:
  41.  
  42.      [EINVAL]       bp is not a valid BLKFILE pointer.
  43.      [EINVAL]       bufsize is less than 1.
  44.      [EINVAL]       buf is the NULL pointer.
  45.      [BEBOUND]      offset + bufsize extends beyond the
  46.                     boundary of the header.
  47.      [BEEOF]        Attempt to write a field before the
  48.                     complete header has been written.
  49.      [BENOPEN]      bp is not open for writing.
  50.  
  51. SEE ALSO
  52.      bgethf, bputh, bputbf.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. NOTES
  59.      When the header is created,  the entire header must be written,
  60.      so offset must have a value of 0 and bufsize must be equal to the
  61.      block size.
  62.  
  63. ------------------------------------------------------------------------------*/
  64. #ifdef AC_PROTO
  65. int bputhf(BLKFILE *bp, size_t offset, const void *buf, size_t bufsize)
  66. #else
  67. int bputhf(bp, offset, buf, bufsize)
  68. BLKFILE *bp;
  69. size_t offset;
  70. const void *buf;
  71. size_t bufsize;
  72. #endif
  73. {
  74.     /* validate arguments */
  75.     if (!b_valid(bp) || buf == NULL || bufsize < 1) {
  76.         errno = EINVAL;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if not open for reading */
  81.     if (!(bp->flags & BIOWRITE)) {
  82.         errno = BENOPEN;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if block boundary is crossed */
  87.     if ((offset + bufsize) > bp->hdrsize) {
  88.         errno = BEBOUND;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if incomplete header */
  93.     if (offset != 0 || bufsize != bp->hdrsize) {
  94.         if (bp->endblk < 1) {
  95.             errno = BEEOF;
  96.             return -1;
  97.         }
  98.     }
  99.  
  100.     /* check if not buffered */
  101.     if (bp->bufcnt == 0) {
  102.         if (b_uputf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  103.             BEPRINT;
  104.             return -1;
  105.         }
  106.         if (bp->endblk < 1) {
  107.             bp->endblk = 1;
  108.         }
  109.         return 0;
  110.     }
  111.  
  112.     /* check if buffer is not loaded */
  113.     if (!(b_blockp(bp, (size_t)0)->flags & BLKREAD)) {
  114.         if (offset != 0 || bufsize != bp->hdrsize) {
  115.             if (b_get(bp, (size_t)0) == -1) {    /* read block from file */
  116.                 if (errno != BEEOF) BEPRINT;
  117.                 return -1;
  118.             }
  119.         }
  120.     }
  121.  
  122.     /* copy from buf into block buffer and set flags */
  123.     memcpy(((char *)b_blkbuf(bp, (size_t)0) + offset), buf, bufsize);
  124.     b_blockp(bp, (size_t)0)->flags = BLKREAD | BLKWRITE;
  125.  
  126.     /* adjust endblk */
  127.     if (bp->endblk < 1) {
  128.         bp->endblk = 1;
  129.     }
  130.  
  131.     return 0;
  132. }
  133.